package com.anxin.registerManagement.controller;

import com.anxin.registerManagement.bean.dto.RoomDto;
import com.anxin.registerManagement.bean.jo.RoomSearchJo;
import com.anxin.registerManagement.bean.persist.Room;
import com.anxin.registerManagement.service.RoomService;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author 叶前呈
 * @date 2022/1/30 14:32
 */
@Api(tags = "房间接口")
@RestController
@RequestMapping("/rooms")
public class RoomController {

    private static final Logger logger = LoggerFactory.getLogger(RoomController.class);

    @Autowired
    RoomService roomService;

    @ApiOperation("查询房间")
    @PostMapping("/search")
    public ResultUtil getRoomList(@RequestBody RoomSearchJo roomSearchJo) {
        logger.info("查询房间：{}", roomSearchJo);
        return ResultUtil.success(roomService.getRoomList(roomSearchJo));
    }

    @ApiOperation("添加房间")
    @PostMapping("/add")
    public ResultUtil addRoom(@Valid @RequestBody Room room) {
        roomService.addRoom(room);
        return ResultUtil.success();
    }

    @ApiOperation("修改房间")
    @PostMapping("/update")
    public ResultUtil updateRoom(@Valid @RequestBody Room room) {
        Assert.notNull(room.getId(), "房间id不能为空");
        roomService.updateRoom(room);
        return ResultUtil.success();
    }

    @ApiOperation("删除房间")
    @PostMapping("/delete")
    public ResultUtil deleteRoom(@RequestParam("id") int id) {
        Assert.notNull(id, "房间id不能为空");
        roomService.deleteRoom(id);
        return ResultUtil.success();
    }

    @ApiOperation("获取当前用户房间信息")
    @GetMapping("/getRoomByUser")
    public ResultUtil getRoomByUser() {
        return ResultUtil.success(roomService.getRoomByUser());
    }

    @ApiOperation("入住")
    @PostMapping("/checkIn")
    public ResultUtil checkIn(@RequestParam("roomId") Integer roomId) {
        Assert.notNull(roomId, "房间id不能为空");
        roomService.checkIn(roomId);
        return ResultUtil.success();
    }

    @ApiOperation("退房")
    @PostMapping("/checkOut")
    public ResultUtil checkOut(@RequestParam("roomId") Integer roomId) {
        Assert.notNull(roomId, "房间id不能为空");
        roomService.checkOut(roomId);
        return ResultUtil.success();
    }

    @ApiOperation("获取所有房间")
    @GetMapping("/get")
    public ResultUtil getAllRooms() {
        return ResultUtil.success(roomService.getAllRooms());
    }

}
